﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

namespace AIC_KQKT
{
    public partial class BaoCaoMau12 : MyFromBase
    {
        #region PageLoad
        protected void Page_Load(object sender, EventArgs e)
        {
            CheckPermission();
            if (!IsPostBack)
            {
                BindDataToControls();
            }



        }

        /// <summary>
        /// Thủ tục kiểm tra quyền sử dụng của người dùng có được quyền truy cập vào mục này hay không?
        /// </summary>
        private void CheckPermission()
        {
            Dictionary<string, int> Role = MyCommon.CheckPermission("BaoCaoMau12");
            if (Role == null || Role["Xem"] == 0 || ((Role["Xem"] == 1 && Role["Sua"] == 0)))
            {
                Response.Redirect("Index.aspx?p=0");
            }
        }

        /// <summary>
        /// Bind Data vào các contrls như Combobox, ListBox, textBox ...
        /// </summary>
        private void BindDataToControls()
        {
            //TODO
            if (Session["userid"] != null)
            {
                string sql = "Select DV.* " +
                             "From tblDonVi DV " +
                             "Inner Join tblUser U On U.DonViID=DV.ID " +
                             "Where U.ID=" + Session["userid"];
                DataTable dtDV = SqlHelper.ExecuteDataTable(CommandType.Text, sql);
                tdTenDonVi.InnerText = "(" + dtDV.Rows[0]["MaDV"] + ") " + dtDV.Rows[0]["TenDV"];
                sql = "Select ID,TenDM From tblDanhMuc Where MaDM='" + MyConst.DanhMuc.KyBaoCao.Ma + "' ";
                SqlDataReader reKyBaoCao = SqlHelper.ExecuteReader(CommandType.Text, sql);
                clsDataBind.CreateCombo("Tất cả", reKyBaoCao, cboKyBaoCao, 1, 0);
            }
        }

        protected void BindDataToTextField()
        {
            string sql = "SELECT * FROM dbo.tblBaoCaoMau10 WHERE KyBaoCaoID_DM = @KyBaoCaoID_DM AND DonViID = @DonViID AND IsCommon = 1";
            List<SqlParameter> lstParameters = new List<SqlParameter>();
            lstParameters.Add(SqlHelper.CreateParameterObject("@KyBaoCaoID_DM", SqlDbType.Int, ParameterDirection.Input, cboKyBaoCao.SelectedValue));
            lstParameters.Add(SqlHelper.CreateParameterObject("@DonViID", SqlDbType.Int, ParameterDirection.Input, MyCommon.GetDonViID(Session["userid"].ToString())));
            DataTable dt = SqlHelper.ExecuteDataTable(CommandType.Text, sql, lstParameters);
            if (dt.Rows.Count != 0)
            {
                txtSoBaoCao.Text = dt.Rows[0]["CM_SoBaoCao"].ToString();
                txtTenBaoCao.Text = dt.Rows[0]["CM_TenBaoCao"].ToString();
                txtNguoiLapBaoCao.Text = dt.Rows[0]["CM_NguoiLapBaoCao"].ToString();
                txtNgayBaoCao.Text = dt.Rows[0]["CM_NgayBaoCao"].Equals(DBNull.Value) ? "" : Convert.ToDateTime(dt.Rows[0]["CM_NgayBaoCao"]).ToString("dd/MM/yyyy");
                txtThuTruong.Text = dt.Rows[0]["CM_ThuTruong"].ToString();
                txtFrom.Text = dt.Rows[0]["CM_TuNgay"].Equals(DBNull.Value) ? "" : dt.Rows[0]["CM_TuNgay"].ToString();
                txtTo.Text = dt.Rows[0]["CM_DenNgay"].Equals(DBNull.Value) ? "" : dt.Rows[0]["CM_DenNgay"].ToString();
            }

        }

        /// <summary>
        /// Bind Data vào gridview
        /// </summary>
        private void BindDataToGrid3()
        {
            bool IsEmpty = false;
            List<SqlParameter> arrParams = new List<SqlParameter>();
            arrParams.Add(SqlHelper.CreateParameterObject("@KyBaoCaoID_DM", SqlDbType.Int, ParameterDirection.Input, cboKyBaoCao.SelectedValue));
            arrParams.Add(SqlHelper.CreateParameterObject("@DonViID", SqlDbType.Int, ParameterDirection.Input, MyCommon.GetDonViID(Session["userid"].ToString())));

            string sql = "SELECT * FROM dbo.tblBaoCaoMau12 WHERE DonViID = @DonViID AND KyBaoCaoID_DM = @KyBaoCaoID_DM AND LaToChuc = 0";
            DataTable dt = SqlHelper.ExecuteDataTable(CommandType.Text, sql, arrParams);
            if (dt.Rows.Count == 0)
            {
                IsEmpty = true;
                DataRow dr = dt.NewRow();
                dr["F1"] = "";
                dr["F2"] = "";
                dr["F3"] = "";
                dr["F4"] = "";
                dr["F5"] = "";
                dr["F6"] = "";
                dr["F7"] = "";
                dr["F8"] = "";
                dr["F9"] = "";
                dt.Rows.InsertAt(dr, 0);

            }
            grvData3.ShowFooter = true;
            grvData3.DataSource = dt;
            grvData3.DataBind();
            if (IsEmpty)
            {
                grvData3.Rows[0].Visible = false;
            }
        }

        /// <summary>
        /// Bind Data vào gridview
        /// </summary>
        private void BindDataToGrid4()
        {
            bool IsEmpty = false;
            List<SqlParameter> arrParams = new List<SqlParameter>();
            arrParams.Add(SqlHelper.CreateParameterObject("@KyBaoCaoID_DM", SqlDbType.Int, ParameterDirection.Input, cboKyBaoCao.SelectedValue));
            arrParams.Add(SqlHelper.CreateParameterObject("@DonViID", SqlDbType.Int, ParameterDirection.Input, MyCommon.GetDonViID(Session["userid"].ToString())));

            string sql = "SELECT * FROM dbo.tblBaoCaoMau12 WHERE DonViID = @DonViID AND KyBaoCaoID_DM = @KyBaoCaoID_DM AND LaToChuc = 1";
            DataTable dt = SqlHelper.ExecuteDataTable(CommandType.Text, sql, arrParams);
            if (dt.Rows.Count == 0)
            {
                IsEmpty = true;
                DataRow dr = dt.NewRow();
                dr["F1"] = "";
                dr["F2"] = "";
                dr["F3"] = "";
                dr["F4"] = "";
                dr["F5"] = "";
                dr["F6"] = "";
                dr["F7"] = "";
                dr["F8"] = "";
                dr["F9"] = "";
                dr["F10"] = "";
                dr["F11"] = "";
                dr["F12"] = "";
                dr["F13"] = "";
                dr["F14"] = "";
                dr["F15"] = "";
                dr["F16"] = "";
                dr["F17"] = "";
                dt.Rows.InsertAt(dr, 0);

            }
            grvData4.ShowFooter = true;
            grvData4.DataSource = dt;
            grvData4.DataBind();
            if (IsEmpty)
            {
                grvData4.Rows[0].Visible = false;
            }
        }

        #endregion

        #region Events

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("Index.aspx");
        }

        protected void cboKyBaoCao_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cboKyBaoCao.SelectedIndex > 0)
            {
                btnLuu.Visible = true;
                BindDataToGrid3();
                BindDataToGrid4();
                BindDataToTextField();
            }
            else
            {
                btnLuu.Visible = false;
                grvData3.Visible = false;
                grvData4.Visible = false;
                ResetField();
            }
        }

        private void ResetField()
        {
            txtSoBaoCao.Text = "";
            txtTenBaoCao.Text = "";
            txtFrom.Text = "";
            txtTo.Text = "";
            txtNguoiLapBaoCao.Text = "";
            txtNgayBaoCao.Text = "";
            txtThuTruong.Text = "";
        }

        #endregion

        #region GrvData3 Command
        protected void grvData3_RowCreated(object sender, GridViewRowEventArgs e)
        {

            if (e.Row.RowType == DataControlRowType.Header)
            {

                GridView objGridView = (GridView)sender;
                //GridViewRow objgridviewrow = new GridViewRow(1, 0, DataControlRowType.Header, DataControlRowState.Insert);

                GridViewRow objgridviewrow = new GridViewRow(1, 0, DataControlRowType.Header, DataControlRowState.Insert);
                GridViewRow objgridviewrow2 = new GridViewRow(1, 0, DataControlRowType.Header, DataControlRowState.Insert);

                TableCell objtablecell = new TableCell();
                AddMergedCells(objgridviewrow, objtablecell, 1, 2, "SỐ TT");
                AddMergedCells(objgridviewrow, objtablecell, 1, 2, "TÊN TỔ CHỨC ĐẢNG - HỌ VÀ TÊN ĐẢNG VIÊN");
                AddMergedCells(objgridviewrow, objtablecell, 1, 2, "ĐƠN VỊ");
                AddMergedCells(objgridviewrow, objtablecell, 1, 2, "CẤP BẬC");
                AddMergedCells(objgridviewrow, objtablecell, 2, 1, "CHỨC VỤ");
                AddMergedCells(objgridviewrow, objtablecell, 1, 2, "TÓM TẮT NỘI DUNG VI PHẠM (ghi rõ nội dung, thời gian, mức độ vi phạm)");
                AddMergedCells(objgridviewrow, objtablecell, 3, 1, "HÌNH THỨC, CẤP, SỐ QUYẾT ĐỊNH");
                AddMergedCells(objgridviewrow, objtablecell, 1, 2, "Sửa");
                objgridviewrow.BackColor = System.Drawing.Color.FromArgb(0x00, 0x45, 0x8A);
                objgridviewrow.ForeColor = System.Drawing.Color.White;
                objgridviewrow.Font.Bold = true;
                objGridView.Controls[0].Controls.AddAt(0, objgridviewrow);

                objtablecell = new TableCell();
                AddMergedCells(objgridviewrow2, objtablecell, 1, 1, "ĐẢNG");
                AddMergedCells(objgridviewrow2, objtablecell, 1, 1, "CHÍNH QUYỀN");
                AddMergedCells(objgridviewrow2, objtablecell, 1, 1, "VỀ ĐẢNG");
                AddMergedCells(objgridviewrow2, objtablecell, 1, 1, "VỀ CHÍNH QUYỀN");
                AddMergedCells(objgridviewrow2, objtablecell, 1, 1, "VỀ PHÁP LUẬT");
                objgridviewrow2.BackColor = System.Drawing.Color.FromArgb(0x00, 0x45, 0x8A);
                objgridviewrow2.ForeColor = System.Drawing.Color.White;
                objgridviewrow2.Font.Bold = true;
                objGridView.Controls[0].Controls.AddAt(1, objgridviewrow2);
            }
        }

        protected void grvData3_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            RowDataBound(sender, e);
        }

        protected void grvData3_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string sID = e.CommandArgument.ToString().Trim();
            string sCommandName = e.CommandName;


            if ((sCommandName == AIC_KQKT.MyConst.Action.Approve))
            {
                SqlTransaction MyTrans = SqlHelper.BeginTransaction();
                try
                {

                    // cap nhat vao database
                    TextBox txtF1 = (TextBox)grvData3.Rows[grvData3.EditIndex].FindControl("txtF1");
                    TextBox txtF2 = (TextBox)grvData3.Rows[grvData3.EditIndex].FindControl("txtF2");
                    TextBox txtF3 = (TextBox)grvData3.Rows[grvData3.EditIndex].FindControl("txtF3");
                    TextBox txtF4 = (TextBox)grvData3.Rows[grvData3.EditIndex].FindControl("txtF4");
                    TextBox txtF5 = (TextBox)grvData3.Rows[grvData3.EditIndex].FindControl("txtF5");
                    TextBox txtF6 = (TextBox)grvData3.Rows[grvData3.EditIndex].FindControl("txtF6");
                    TextBox txtF7 = (TextBox)grvData3.Rows[grvData3.EditIndex].FindControl("txtF7");
                    TextBox txtF8 = (TextBox)grvData3.Rows[grvData3.EditIndex].FindControl("txtF8");
                    TextBox txtF9 = (TextBox)grvData3.Rows[grvData3.EditIndex].FindControl("txtF9");

                    string sSQL = "UPDATE dbo.tblBaoCaoMau12 " +
                                  "SET F1=@F1,F2=@F2,F3=@F3,F4=@F4,F5=@F5,F6=@F6,F7=@F7,F8=@F8,F9=@F9" +
                                  ",UpdateDate = GETDATE(),UserID = @UserID WHERE ID = @ID";

                    List<SqlParameter> lstParameter = new List<SqlParameter>();

                    lstParameter.Add(SqlHelper.CreateParameterObject("@F1", SqlDbType.NVarChar, ParameterDirection.Input, txtF1.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F2", SqlDbType.NVarChar, ParameterDirection.Input, txtF2.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F3", SqlDbType.NVarChar, ParameterDirection.Input, txtF3.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F4", SqlDbType.NVarChar, ParameterDirection.Input, txtF4.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F5", SqlDbType.NVarChar, ParameterDirection.Input, txtF5.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F6", SqlDbType.NVarChar, ParameterDirection.Input, txtF6.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F7", SqlDbType.NVarChar, ParameterDirection.Input, txtF7.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F8", SqlDbType.NVarChar, ParameterDirection.Input, txtF8.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F9", SqlDbType.NVarChar, ParameterDirection.Input, txtF9.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@UserID", SqlDbType.Int, ParameterDirection.Input, Session["userid"]));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@ID", SqlDbType.Int, ParameterDirection.Input, sID));

                    SqlHelper.ExecuteNonQuery(CommandType.Text, sSQL, lstParameter);
                    MyTrans.Commit();
                    grvData3.EditIndex = -1;
                    BindDataToGrid3();
                    MyCommon.AddMessage(this.Master, "Cập nhật thành công", MyConst.MessageType.Success);
                }
                catch (Exception)
                {
                    MyTrans.Rollback();
                }
            }
            else if (e.CommandName == AIC_KQKT.MyConst.Action.Addnew)
            {
                SqlTransaction MyTrans = SqlHelper.BeginTransaction();
                try
                {
                    TextBox txtF1New = (TextBox)grvData3.FooterRow.FindControl("txtF1New");
                    TextBox txtF2New = (TextBox)grvData3.FooterRow.FindControl("txtF2New");
                    TextBox txtF3New = (TextBox)grvData3.FooterRow.FindControl("txtF3New");
                    TextBox txtF4New = (TextBox)grvData3.FooterRow.FindControl("txtF4New");
                    TextBox txtF5New = (TextBox)grvData3.FooterRow.FindControl("txtF5New");
                    TextBox txtF6New = (TextBox)grvData3.FooterRow.FindControl("txtF6New");
                    TextBox txtF7New = (TextBox)grvData3.FooterRow.FindControl("txtF7New");
                    TextBox txtF8New = (TextBox)grvData3.FooterRow.FindControl("txtF8New");
                    TextBox txtF9New = (TextBox)grvData3.FooterRow.FindControl("txtF9New");

                    string sql = "INSERT INTO dbo.tblBaoCaoMau12( KyBaoCaoID_DM ,DonViID ,F1 ,F2 ,F3 ,F4 ,F5 ,F6 ,F7 ,F8 ,F9 ,CreateDate ,UpdateDate ,UserID, LaToChuc) " +
                                 "VALUES  ( @KyBaoCaoID_DM,@DonViID ,@F1,@F2,@F3,@F4,@F5,@F6,@F7,@F8,@F9,GETDATE(),GETDATE(),@UserID, 0)";
                    List<SqlParameter> lstParameter = new List<SqlParameter>();
                    lstParameter.Add(SqlHelper.CreateParameterObject("@KyBaoCaoID_DM", SqlDbType.Int, ParameterDirection.Input, Convert.ToInt32(cboKyBaoCao.SelectedValue)));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@DonViID", SqlDbType.Int, ParameterDirection.Input, MyCommon.GetDonViID(Session["userid"].ToString())));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F1", SqlDbType.NVarChar, ParameterDirection.Input, txtF1New.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F2", SqlDbType.NVarChar, ParameterDirection.Input, txtF2New.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F3", SqlDbType.NVarChar, ParameterDirection.Input, txtF3New.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F4", SqlDbType.NVarChar, ParameterDirection.Input, txtF4New.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F5", SqlDbType.NVarChar, ParameterDirection.Input, txtF5New.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F6", SqlDbType.NVarChar, ParameterDirection.Input, txtF6New.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F7", SqlDbType.NVarChar, ParameterDirection.Input, txtF7New.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F8", SqlDbType.NVarChar, ParameterDirection.Input, txtF8New.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F9", SqlDbType.NVarChar, ParameterDirection.Input, txtF9New.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@UserID", SqlDbType.Int, ParameterDirection.Input, Convert.ToInt32(Session["userid"].ToString())));

                    int Result = SqlHelper.ExecuteNonQuery(CommandType.Text, sql, lstParameter);
                    if (Result == 0)
                    {
                        MyCommon.AddMessage(this.Master, "Có lỗi xảy ra trong quá trình thêm mới", MyConst.MessageType.Error);
                    }
                    else
                    {
                        MyCommon.AddMessage(this.Master, "Thêm mới thành công !", MyConst.MessageType.Success);
                    }

                    MyTrans.Commit();
                    BindDataToGrid3();
                }
                catch (Exception)
                {
                    MyCommon.AddMessage(this.Master, "Có lỗi xảy ra trong quá trình thêm mới", MyConst.MessageType.Error);
                    MyTrans.Rollback();
                }
            }
            else if ((sCommandName == AIC_KQKT.MyConst.Action.Delete))
            {
                string sql = "DELETE FROM dbo.tblBaoCaoMau12 WHERE ID = @ID";
                List<SqlParameter> lstParameter = new List<SqlParameter>();

                lstParameter.Add(SqlHelper.CreateParameterObject("@ID", SqlDbType.Int, ParameterDirection.Input, sID));
                SqlHelper.ExecuteNonQuery(CommandType.Text, sql, lstParameter);

                grvData3.EditIndex = -1;
                BindDataToGrid3();
                MyCommon.AddMessage(this.Master, "Xóa thành công !", MyConst.MessageType.Success);
            }
        }

        protected void grvData3_RowEditing(object sender, GridViewEditEventArgs e)
        {

            grvData3.EditIndex = e.NewEditIndex;
            BindDataToGrid3();
        }

        protected void grvData3_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            grvData3.EditIndex = -1;
            BindDataToGrid3();
        } 
        #endregion

        #region GrvData4 Command
        protected void grvData4_RowCreated(object sender, GridViewRowEventArgs e)
        {

            if (e.Row.RowType == DataControlRowType.Header)
            {

                GridView objGridView = (GridView)sender;
                //GridViewRow objgridviewrow = new GridViewRow(1, 0, DataControlRowType.Header, DataControlRowState.Insert);

                GridViewRow objgridviewrow = new GridViewRow(1, 0, DataControlRowType.Header, DataControlRowState.Insert);
                GridViewRow objgridviewrow2 = new GridViewRow(1, 0, DataControlRowType.Header, DataControlRowState.Insert);
                GridViewRow objgridviewrow3 = new GridViewRow(1, 0, DataControlRowType.Header, DataControlRowState.Insert);

                TableCell objtablecell = new TableCell();
                AddMergedCells(objgridviewrow, objtablecell, 1, 2, "SỐ TT");
                AddMergedCells(objgridviewrow, objtablecell, 1, 2, "TÊN TỔ CHỨC - HỌ VÀ TÊN ĐẢNG VIÊN");
                AddMergedCells(objgridviewrow, objtablecell, 1, 2, "ĐƠN VỊ");
                AddMergedCells(objgridviewrow, objtablecell, 1, 2, "CẤP BẤC");
                AddMergedCells(objgridviewrow, objtablecell, 2, 1, "CHỨC VỤ");
                AddMergedCells(objgridviewrow, objtablecell, 1, 2, "THÁNG NĂM BỊ TỐ CÁO");
                AddMergedCells(objgridviewrow, objtablecell, 7, 1, "KẾT LUẬN");
                AddMergedCells(objgridviewrow, objtablecell, 1, 2, "Cấp và tháng năm giải quyết");
                AddMergedCells(objgridviewrow, objtablecell, 1, 2, "TÓM TẮT NỘI DUNG TỐ CÁO");
                AddMergedCells(objgridviewrow, objtablecell, 2, 1, "HÌNH THỨC XỬ LÝ");
                AddMergedCells(objgridviewrow, objtablecell, 1, 2, "Sửa");
                objgridviewrow.BackColor = System.Drawing.Color.FromArgb(0x00, 0x45, 0x8A);
                objgridviewrow.ForeColor = System.Drawing.Color.White;
                objgridviewrow.Font.Bold = true;
                objGridView.Controls[0].Controls.AddAt(0, objgridviewrow);

                objtablecell = new TableCell();
                AddMergedCells(objgridviewrow2, objtablecell, 1, 1, "ĐẢNG");
                AddMergedCells(objgridviewrow2, objtablecell, 1, 1, "CHÍNH QUYỀN");
                AddMergedCells(objgridviewrow2, objtablecell, 1, 1, "Chưa đủ căn cứ kết luận");
                AddMergedCells(objgridviewrow2, objtablecell, 1, 1, "Tố sai");
                AddMergedCells(objgridviewrow2, objtablecell, 1, 1, "Tố đúng một phần");
                AddMergedCells(objgridviewrow2, objtablecell, 1, 1, "Tố đúng hoàn toàn");
                AddMergedCells(objgridviewrow2, objtablecell, 1, 1, "Có vi phạm");
                AddMergedCells(objgridviewrow2, objtablecell, 1, 1, "Phải kỷ luật");
                AddMergedCells(objgridviewrow2, objtablecell, 1, 1, "Đã kỷ luật");
                AddMergedCells(objgridviewrow2, objtablecell, 1, 1, "Kỷ luật Đảng");
                AddMergedCells(objgridviewrow2, objtablecell, 1, 1, "Kỷ luật chính quyền");
                objgridviewrow2.BackColor = System.Drawing.Color.FromArgb(0x00, 0x45, 0x8A);
                objgridviewrow2.ForeColor = System.Drawing.Color.White;
                objgridviewrow2.Font.Bold = true;
                objGridView.Controls[0].Controls.AddAt(1, objgridviewrow2);
            }
        }

        protected void grvData4_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            RowDataBound(sender, e);
        }

        protected void grvData4_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string sID = e.CommandArgument.ToString().Trim();
            string sCommandName = e.CommandName;


            if ((sCommandName == AIC_KQKT.MyConst.Action.Approve))
            {
                SqlTransaction MyTrans = SqlHelper.BeginTransaction();
                try
                {

                    // cap nhat vao database
                    TextBox txtF1 = (TextBox)grvData4.Rows[grvData4.EditIndex].FindControl("txtF1");
                    TextBox txtF2 = (TextBox)grvData4.Rows[grvData4.EditIndex].FindControl("txtF2");
                    TextBox txtF3 = (TextBox)grvData4.Rows[grvData4.EditIndex].FindControl("txtF3");
                    TextBox txtF4 = (TextBox)grvData4.Rows[grvData4.EditIndex].FindControl("txtF4");
                    TextBox txtF5 = (TextBox)grvData4.Rows[grvData4.EditIndex].FindControl("txtF5");
                    TextBox txtF6 = (TextBox)grvData4.Rows[grvData4.EditIndex].FindControl("txtF6");
                    TextBox txtF7 = (TextBox)grvData4.Rows[grvData4.EditIndex].FindControl("txtF7");
                    TextBox txtF8 = (TextBox)grvData4.Rows[grvData4.EditIndex].FindControl("txtF8");
                    TextBox txtF9 = (TextBox)grvData4.Rows[grvData4.EditIndex].FindControl("txtF9");
                    TextBox txtF10 = (TextBox)grvData4.Rows[grvData4.EditIndex].FindControl("txtF10");
                    TextBox txtF11 = (TextBox)grvData4.Rows[grvData4.EditIndex].FindControl("txtF11");
                    TextBox txtF12 = (TextBox)grvData4.Rows[grvData4.EditIndex].FindControl("txtF12");
                    TextBox txtF13 = (TextBox)grvData4.Rows[grvData4.EditIndex].FindControl("txtF13");
                    TextBox txtF14 = (TextBox)grvData4.Rows[grvData4.EditIndex].FindControl("txtF14");
                    TextBox txtF15 = (TextBox)grvData4.Rows[grvData4.EditIndex].FindControl("txtF15");
                    TextBox txtF16 = (TextBox)grvData4.Rows[grvData4.EditIndex].FindControl("txtF16");
                    TextBox txtF17 = (TextBox)grvData4.Rows[grvData4.EditIndex].FindControl("txtF17");

                    string sSQL = "UPDATE dbo.tblBaoCaoMau12 " +
                                  "SET F1=@F1,F2=@F2,F3=@F3,F4=@F4,F5=@F5,F6=@F6,F7=@F7,F8=@F8,F9=@F9,F10=@F10,F11=@F11,F12=@F12,F13=@F13,F14=@F14,F15=@F15" +
                                  ",F16=@F16,F17=@F17,UpdateDate = GETDATE(),UserID = @UserID WHERE ID = @ID";

                    List<SqlParameter> lstParameter = new List<SqlParameter>();

                    lstParameter.Add(SqlHelper.CreateParameterObject("@F1", SqlDbType.NVarChar, ParameterDirection.Input, txtF1.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F2", SqlDbType.NVarChar, ParameterDirection.Input, txtF2.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F3", SqlDbType.NVarChar, ParameterDirection.Input, txtF3.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F4", SqlDbType.NVarChar, ParameterDirection.Input, txtF4.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F5", SqlDbType.NVarChar, ParameterDirection.Input, txtF5.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F6", SqlDbType.NVarChar, ParameterDirection.Input, txtF6.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F7", SqlDbType.NVarChar, ParameterDirection.Input, txtF7.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F8", SqlDbType.NVarChar, ParameterDirection.Input, txtF8.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F9", SqlDbType.NVarChar, ParameterDirection.Input, txtF9.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F10", SqlDbType.NVarChar, ParameterDirection.Input, txtF10.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F11", SqlDbType.NVarChar, ParameterDirection.Input, txtF11.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F12", SqlDbType.NVarChar, ParameterDirection.Input, txtF12.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F13", SqlDbType.NVarChar, ParameterDirection.Input, txtF13.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F14", SqlDbType.NVarChar, ParameterDirection.Input, txtF14.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F15", SqlDbType.NVarChar, ParameterDirection.Input, txtF15.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F16", SqlDbType.NVarChar, ParameterDirection.Input, txtF16.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F17", SqlDbType.NVarChar, ParameterDirection.Input, txtF17.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@UserID", SqlDbType.Int, ParameterDirection.Input, Session["userid"]));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@ID", SqlDbType.Int, ParameterDirection.Input, sID));

                    SqlHelper.ExecuteNonQuery(CommandType.Text, sSQL, lstParameter);
                    MyTrans.Commit();
                    grvData4.EditIndex = -1;
                    BindDataToGrid4();
                    MyCommon.AddMessage(this.Master, "Cập nhật thành công", MyConst.MessageType.Success);
                }
                catch (Exception)
                {
                    MyTrans.Rollback();
                }
            }
            else if (e.CommandName == AIC_KQKT.MyConst.Action.Addnew)
            {
                SqlTransaction MyTrans = SqlHelper.BeginTransaction();
                try
                {
                    TextBox txtF1New = (TextBox)grvData4.FooterRow.FindControl("txtF1New");
                    TextBox txtF2New = (TextBox)grvData4.FooterRow.FindControl("txtF2New");
                    TextBox txtF3New = (TextBox)grvData4.FooterRow.FindControl("txtF3New");
                    TextBox txtF4New = (TextBox)grvData4.FooterRow.FindControl("txtF4New");
                    TextBox txtF5New = (TextBox)grvData4.FooterRow.FindControl("txtF5New");
                    TextBox txtF6New = (TextBox)grvData4.FooterRow.FindControl("txtF6New");
                    TextBox txtF7New = (TextBox)grvData4.FooterRow.FindControl("txtF7New");
                    TextBox txtF8New = (TextBox)grvData4.FooterRow.FindControl("txtF8New");
                    TextBox txtF9New = (TextBox)grvData4.FooterRow.FindControl("txtF9New");
                    TextBox txtF10New = (TextBox)grvData4.FooterRow.FindControl("txtF10New");
                    TextBox txtF11New = (TextBox)grvData4.FooterRow.FindControl("txtF11New");
                    TextBox txtF12New = (TextBox)grvData4.FooterRow.FindControl("txtF12New");
                    TextBox txtF13New = (TextBox)grvData4.FooterRow.FindControl("txtF13New");
                    TextBox txtF14New = (TextBox)grvData4.FooterRow.FindControl("txtF14New");
                    TextBox txtF15New = (TextBox)grvData4.FooterRow.FindControl("txtF15New");
                    TextBox txtF16New = (TextBox)grvData4.FooterRow.FindControl("txtF16New");
                    TextBox txtF17New = (TextBox)grvData4.FooterRow.FindControl("txtF17New");

                    string sql = "INSERT INTO dbo.tblBaoCaoMau12( KyBaoCaoID_DM ,DonViID,F1,F2,F3,F4,F5,F6,F7,F8,F9,F10,F11,F12,F13,F14,F15,F16,F17,CreateDate ,UpdateDate ,UserID, LaToChuc) " +
                                 "VALUES  ( @KyBaoCaoID_DM,@DonViID ,@F1,@F2,@F3,@F4,@F5,@F6,@F7,@F8,@F9,@F10,@F11,@F12,@F13,@F14,@F15,@F16,@F17,GETDATE(),GETDATE(),@UserID,1)";
                    List<SqlParameter> lstParameter = new List<SqlParameter>();
                    lstParameter.Add(SqlHelper.CreateParameterObject("@KyBaoCaoID_DM", SqlDbType.Int, ParameterDirection.Input, Convert.ToInt32(cboKyBaoCao.SelectedValue)));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@DonViID", SqlDbType.Int, ParameterDirection.Input, MyCommon.GetDonViID(Session["userid"].ToString())));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F1", SqlDbType.NVarChar, ParameterDirection.Input, txtF1New.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F2", SqlDbType.NVarChar, ParameterDirection.Input, txtF2New.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F3", SqlDbType.NVarChar, ParameterDirection.Input, txtF3New.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F4", SqlDbType.NVarChar, ParameterDirection.Input, txtF4New.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F5", SqlDbType.NVarChar, ParameterDirection.Input, txtF5New.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F6", SqlDbType.NVarChar, ParameterDirection.Input, txtF6New.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F7", SqlDbType.NVarChar, ParameterDirection.Input, txtF7New.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F8", SqlDbType.NVarChar, ParameterDirection.Input, txtF8New.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F9", SqlDbType.NVarChar, ParameterDirection.Input, txtF9New.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F10", SqlDbType.NVarChar, ParameterDirection.Input, txtF10New.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F11", SqlDbType.NVarChar, ParameterDirection.Input, txtF11New.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F12", SqlDbType.NVarChar, ParameterDirection.Input, txtF12New.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F13", SqlDbType.NVarChar, ParameterDirection.Input, txtF13New.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F14", SqlDbType.NVarChar, ParameterDirection.Input, txtF14New.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F15", SqlDbType.NVarChar, ParameterDirection.Input, txtF15New.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F16", SqlDbType.NVarChar, ParameterDirection.Input, txtF16New.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F17", SqlDbType.NVarChar, ParameterDirection.Input, txtF17New.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@UserID", SqlDbType.Int, ParameterDirection.Input, Convert.ToInt32(Session["userid"].ToString())));

                    int Result = SqlHelper.ExecuteNonQuery(CommandType.Text, sql, lstParameter);
                    if (Result == 0)
                    {
                        MyCommon.AddMessage(this.Master, "Có lỗi xảy ra trong quá trình thêm mới", MyConst.MessageType.Error);
                    }
                    else
                    {
                        MyCommon.AddMessage(this.Master, "Thêm mới thành công !", MyConst.MessageType.Success);
                    }

                    MyTrans.Commit();
                    BindDataToGrid4();
                }
                catch (Exception)
                {
                    MyCommon.AddMessage(this.Master, "Có lỗi xảy ra trong quá trình thêm mới", MyConst.MessageType.Error);
                    MyTrans.Rollback();
                }
            }
            else if ((sCommandName == AIC_KQKT.MyConst.Action.Delete))
            {
                string sql = "DELETE FROM dbo.tblBaoCaoMau2 WHERE ID = @ID";
                List<SqlParameter> lstParameter = new List<SqlParameter>();

                lstParameter.Add(SqlHelper.CreateParameterObject("@ID", SqlDbType.Int, ParameterDirection.Input, sID));
                SqlHelper.ExecuteNonQuery(CommandType.Text, sql, lstParameter);

                grvData4.EditIndex = -1;
                BindDataToGrid4();
                MyCommon.AddMessage(this.Master, "Xóa thành công !", MyConst.MessageType.Success);
            }
        }

        protected void grvData4_RowEditing(object sender, GridViewEditEventArgs e)
        {

            grvData4.EditIndex = e.NewEditIndex;
            BindDataToGrid4();
        }

        protected void grvData4_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            grvData4.EditIndex = -1;
            BindDataToGrid4();
        }
        #endregion

        private static void AddMergedCells(GridViewRow objgridviewrow, TableCell objtablecell, int colspan, int rowspan, string celltext)
        {
            objtablecell = new TableCell();
            objtablecell.Text = celltext;
            objtablecell.ColumnSpan = colspan;
            objtablecell.RowSpan = rowspan;
            objtablecell.HorizontalAlign = HorizontalAlign.Center;
            objgridviewrow.Cells.Add(objtablecell);
        }

        protected void btnLuu_Click(object sender, EventArgs e)
        {
            string sSQL = "Update tblBaoCaoMau12 Set CM_SoBaoCao=@CM_SoBaoCao,CM_TenBaoCao = @CM_TenBaoCao,CM_TuNgay = @CM_TuNgay,CM_DenNgay = @CM_DenNgay,CM_ThuTruong = @CM_ThuTruong,CM_NguoiLapBaoCao = @CM_NguoiLapBaoCao,CM_NgayBaoCao = @CM_NgayBaoCao,UpdateDate=GETDATE() " +
                          "Where IsCommon=1 And DonViID=" + MyCommon.GetDonViID(Session["userid"].ToString()) + " And KyBaoCaoID_DM=" + cboKyBaoCao.SelectedValue;

            List<SqlParameter> lstParameter = new List<SqlParameter>();
            lstParameter.Add(SqlHelper.CreateParameterObject("@CM_SoBaoCao", SqlDbType.NVarChar, ParameterDirection.Input, txtSoBaoCao.Text.Trim()));
            lstParameter.Add(SqlHelper.CreateParameterObject("@CM_TenBaoCao", SqlDbType.NVarChar, ParameterDirection.Input, txtTenBaoCao.Text.Trim()));
            lstParameter.Add(SqlHelper.CreateParameterObject("@CM_NguoiLapBaoCao", SqlDbType.NVarChar, ParameterDirection.Input, txtNguoiLapBaoCao.Text.Trim()));
            lstParameter.Add(SqlHelper.CreateParameterObject("@CM_NgayBaoCao", SqlDbType.NVarChar, ParameterDirection.Input, txtNgayBaoCao.Text.Trim()));
            lstParameter.Add(SqlHelper.CreateParameterObject("@CM_ThuTruong", SqlDbType.NVarChar, ParameterDirection.Input, txtThuTruong.Text.Trim()));
            lstParameter.Add(SqlHelper.CreateParameterObject("@CM_TuNgay", SqlDbType.NVarChar, ParameterDirection.Input, txtFrom.Text.Trim()));
            lstParameter.Add(SqlHelper.CreateParameterObject("@CM_DenNgay", SqlDbType.NVarChar, ParameterDirection.Input, txtTo.Text.Trim()));

            try
            {
                SqlHelper.ExecuteNonQuery(CommandType.Text, sSQL, lstParameter);
                MyCommon.AddMessage(this.Master, "Cập nhật báo cáo thành công !", MyConst.MessageType.Success);
            }
            catch (Exception)
            {
                MyCommon.AddMessage(this.Master, "Cập nhật báo cáo lỗi !", MyConst.MessageType.Error);
            }
        }
    }
}
